Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global delay bug fix: check again credits under mutex #438

Merged
merged 1 commit into from
Mar 22, 2023

Conversation

Yuval-Ariel
Copy link
Contributor

i encountered a bug while running tests for the dirty memory connection to the global write controller and the bug is as follows:
during WriteController::GetDelay , we check if there are enough credits and return if there are, here:

auto credits = credit_in_bytes_.load();
  while (credits >= num_bytes) {
    if (credit_in_bytes_.compare_exchange_weak(credits, credits - num_bytes))
      return 0;
 }

if there arent enough credits, we proceed under the mutex but dont check again the condition above which might have changed from a different thread.
this PR adds this additional check and while at it, rename and reorder some members.

@Yuval-Ariel Yuval-Ariel added the bug fix Fixes a known bug label Mar 19, 2023
@Yuval-Ariel Yuval-Ariel requested a review from ayulas March 19, 2023 12:13
@Yuval-Ariel Yuval-Ariel self-assigned this Mar 19, 2023
@@ -148,7 +148,13 @@ uint64_t WriteController::GetDelay(SystemClock* clock, uint64_t num_bytes) {
// Refill every 1 ms
const uint64_t kMicrosPerRefill = 1000;

std::lock_guard<std::mutex> lock(mu_);
std::lock_guard<std::mutex> lock(metrics_mu_);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the use of credit_in_bytes_ is not right as i can see it... as you wrote above
"// This function is no longer under db mutex since credit_in_bytes_ is atomic"
in the first check you does threat it as atomic and do compare_exchange_weak meaning you do change its value not under mutex

but after you take the lock , you changed its value not using atomic operation which is problematic
the lock actually doesnt protect from you from access credit_in_bytes_...
you need to access credit_in_bytes_ with atomic functions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, we'll simply take the mutex before to include the first place where credit_in_bytes_ can change so now only 1 thread can change credit_in_bytes_ simultaneously

@Yuval-Ariel Yuval-Ariel force-pushed the global-bug-fix-credits branch from 92b1d8a to 9361724 Compare March 19, 2023 14:51
ayulas
ayulas previously approved these changes Mar 19, 2023
while at it, rename and reorder some members
@Yuval-Ariel Yuval-Ariel merged commit 19d5597 into main Mar 22, 2023
@Yuval-Ariel Yuval-Ariel deleted the global-bug-fix-credits branch March 23, 2023 08:00
Yuval-Ariel added a commit that referenced this pull request May 2, 2023
While at it, rename and reorder some members
Yuval-Ariel added a commit that referenced this pull request May 4, 2023
While at it, rename and reorder some members
udi-speedb pushed a commit that referenced this pull request Nov 13, 2023
While at it, rename and reorder some members
udi-speedb pushed a commit that referenced this pull request Nov 15, 2023
While at it, rename and reorder some members
udi-speedb pushed a commit that referenced this pull request Dec 4, 2023
While at it, rename and reorder some members
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug fix Fixes a known bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants